<?php

class Database
{
	var $db_host = '';
	var $db_port = 3306;
	var $db_user = '';
	var $db_password = '';
	var $db_database = '';
	
	var $connection;
	var $query_id = '';
	
	var $debug = 0;
	var $noecho = 0;
	var $debug_info = '';
	var $queries = 0;
	var $sql_time = 0;

	//debug
	//var $_page_analyzer = null;

	/*-------------------------------------------------------------------------*/
	// Database(string db_host, string db_port, string db_user, string db_password, string db_database)
	// ------------------
	// Class constructor
	/*-------------------------------------------------------------------------*/
	function Database($db_host, $db_port, $db_user, $db_password, $db_database)
	{
		if (!empty($db_port))
		{
			$this->db_port = $db_port;
		}
		$this->db_host = $db_host;
		$this->db_user = $db_user;
		$this->db_password = $db_password;
		$this->db_database = $db_database;
		$this->debug = isset($_GET['sql_debug']) ? 1 : 0;
		//$this->_page_analyzer = &JyPageAnalyzer::getInstance();
	}
	
	function connect()
	{
		if (!$this->connection || !@mysql_thread_id($this->connection)) {
			$this->connection = @mysql_connect($this->db_host.':'.$this->db_port, $this->db_user, $this->db_password);
			if(!is_resource($this->connection)) {
				$this->error('Cannot connect to database host (' . $this->db_host . ') by user (' . $this->db_user . ').');
			} else {
				if (!@mysql_select_db($this->db_database, $this->connection)) {
					$this->error('Cannot select database (' . $this->db_database . ').');
				}
				mysql_query("SET NAMES 'UTF8'");			
			}
		}
	}

	
	/*-------------------------------------------------------------------------*/
	// resource query(string sqlstr)
	// ------------------
	// Execute an SQL query.
	/*-------------------------------------------------------------------------*/
	function query($sqlstr)
	{
		//printlog($sqlstr);
		$this->connect();
		$this->query_id = @mysql_query($sqlstr, $this->connection);
		if($this->query_id){
			$this->queries++;
			return $this->query_id;
		}
		else {
			printlog($sqlstr, "mysql_error");
		}
		return false;
	}

	/*-------------------------------------------------------------------------*/
	// int get_inc_id(void)
	// ------------------
	// Returns the newly inserted auto_increment id.
	/*-------------------------------------------------------------------------*/
	function get_inc_id()
	{
		$this->connect();
		return mysql_insert_id($this->connection);
	}

	/*-------------------------------------------------------------------------*/
	// mixed result(string sqlstr)
	// ------------------
	// Select a single line or a single cell.
	// eg: $SDB->result("SELECT name FROM user WHERE uid=5")
	//       (returns a string)
	//     $SDB->result("SELECT name,email FROM user WHERE login_hash='$hash'")
	//       (returns an object)
	// Returns NULL if no such entry could be found.
	/*-------------------------------------------------------------------------*/
	function result($sqlstr)
	{
		$this->connect();
		$result = $this->query($sqlstr . " LIMIT 1", $this->connection);
		
		if (!is_resource($result) || mysql_num_rows($result) == 0)
		{
			return NULL;
		}

		if (mysql_num_fields($result) <= 1)
		{
			$arr = mysql_fetch_row($result);
			mysql_free_result($result);
			return $arr[0];
		}
		else
		{
			$obj = mysql_fetch_object($result);
			mysql_free_result($result);
			return $obj;
		}
	}

	/*-------------------------------------------------------------------------*/
	// array fetch_array(resource id)
	// ------------------
	// Fetch a row as an array from result by both assoc and num.
	/*-------------------------------------------------------------------------*/
	function fetch_array($id = NULL)
	{
		$this->connect();
		$queryId = $id ? $id : $this->query_id;
		if(is_resource($queryId)) {
			return 	mysql_fetch_array($queryId);
		} else {
			return NULL;
		}
	}

	/*-------------------------------------------------------------------------*/
	// array fetch_assoc(resource id)
	// ------------------
	// Fetch a row as an array from result by assoc.
	/*-------------------------------------------------------------------------*/
	function fetch_assoc($id = NULL)
	{
		$this->connect();
		
		$queryId = $id ? $id : $this->query_id;
		if(is_resource($queryId)) {
			return 	mysql_fetch_assoc($queryId);
		} else {
			return NULL;
		}		
	}

	/*-------------------------------------------------------------------------*/
	// array fetch_row(resource id)
	// ------------------
	// Fetch a row as an array from result.
	/*-------------------------------------------------------------------------*/
	function fetch_row($id = NULL)
	{
		$this->connect();

		$queryId = $id ? $id : $this->query_id;
		if(is_resource($queryId)) {
			return 	mysql_fetch_row($queryId);
		} else {
			return NULL;
		}				
	}

	/*-------------------------------------------------------------------------*/
	// object fetch_object(resource id)
	// ------------------
	// Fetch a row as an object from result.
	/*-------------------------------------------------------------------------*/
	function fetch_object($id = NULL)
	{
		$this->connect();

		$queryId = $id ? $id : $this->query_id;
		if(is_resource($queryId)) {
			return 	mysql_fetch_object($queryId);
		} else {
			return NULL;
		}						
	}

	/*-------------------------------------------------------------------------*/
	// int num_rows(resource id)
	// ------------------
	// Returns number of collected rows.
	/*-------------------------------------------------------------------------*/
	function num_rows($id = NULL)
	{
		$this->connect();

		$queryId = $id ? $id : $this->query_id;
		if(is_resource($queryId)) {
			return 	mysql_num_rows($queryId);
		} else {
			return 0;
		}								
	}

	/*-------------------------------------------------------------------------*/
	// int affected_rows(resource id)
	// ------------------
	// Returns number of affected rows.
	/*-------------------------------------------------------------------------*/
	function affected_rows()
	{
		return mysql_affected_rows($this->connection);
	}

	/*-------------------------------------------------------------------------*/
	// close_database()
	// ------------------
	// Close Database.
	/*-------------------------------------------------------------------------*/
	function close_database()
	{
		if($this->connection)
		{
			$return = @mysql_close($this->connection);
			$this->connection = false;
			return $return;
		}
	}
	
	//判断某个select 的执行结果是否有记录
	function has_record($sql)
	{
		$this->connect();
		
		$query = $this->query($sql);
		$row = $this->fetch_array();
		if ($row) {
			return true;
		}
		else{
			return false;
		}
	}

}

/*-------------------------------------------------------------------------*/
// string format_string(string str [, bool force])
// ------------------
// Adds slashes into a string for an SQL query.
// Does nothing if magic_quotes_gpc is set on unless forced.
/*-------------------------------------------------------------------------*/
function format_string($str, $force = false)
{
	global $magic_quotes_gpc;

	rtrim($str, '\\');
	if ($force or !$magic_quotes_gpc)
	{
		return strtr($str, array("\\" => "\\\\", "'" => "''"));
	}
	else
	{
		return $str;
	}
}
